# Maintainer: Ray Donnelly <mingw.android@gmail.com>

_realname=metis
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=5.1.0
pkgrel=3
pkgdesc="Serial Graph Partitioning and Fill-reducing Matrix Ordering (mingw-w64)"
arch=('any')
url='http://glaros.dtc.umn.edu/gkhome/views/metis'
license=('Apache')
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake")
options=('strip' 'staticlibs')
source=("http://glaros.dtc.umn.edu/gkhome/fetch/sw/${_realname}/metis-${pkgver}.tar.gz"
        "0001-mingw-w64-does-not-have-sys-resource-h.patch"
        "0002-mingw-w64-do-not-use-reserved-double-underscored-names.patch"
        "0003-WIN32-Install-RUNTIME-to-bin.patch"
        "0004-Fix-GKLIB_PATH-default-for-out-of-tree-builds.patch")
sha256sums=('76faebe03f6c963127dbb73c13eab58c9a3faeae48779f049066a21c087c5db2'
            '2a18b7083c004d2b03fb06c4cda0344002e273daa9d24512da310a7cec350629'
            '779ab9b0fe563a27b78ce2670668fbce4d675e70edb75e1020d299d927e58bf9'
            'a7ad7b4bad7a83c9c72cf5281830ad6e0d50fe6fd07c73734e8c86e762533421'
            'b509590283beb06db544dc76345a3635c8f1eba02d69fcba09f5222add303b9c')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  patch -p1 -i "${srcdir}"/0001-mingw-w64-does-not-have-sys-resource-h.patch
  patch -p1 -i "${srcdir}"/0002-mingw-w64-do-not-use-reserved-double-underscored-names.patch
  patch -p1 -i "${srcdir}"/0003-WIN32-Install-RUNTIME-to-bin.patch
  patch -p1 -i "${srcdir}"/0004-Fix-GKLIB_PATH-default-for-out-of-tree-builds.patch
}

build() {
  for _shared in OFF ON; do
    cd "$srcdir"/${_realname}-${pkgver}
    [[ -d "${srcdir}"/build-shared-${_shared}-${CARCH} ]] && rm -rf "${srcdir}"/build-shared-${_shared}-${CARCH}
    mkdir -p "${srcdir}"/build-shared-${_shared}-${CARCH} && cd "${srcdir}"/build-shared-${_shared}-${CARCH}
    declare -a extra_config
    if check_option "debug" "n"; then
      extra_config+=("-DCMAKE_BUILD_TYPE=Release")
    else
      extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
    fi
    MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX="  \
      ${MINGW_PREFIX}/bin/cmake                    \
        -G'MSYS Makefiles'                         \
        -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX}     \
        -DCMAKE_C_FLAGS="${CPPFLAGS} ${CFLAGS}"    \
        "${extra_config[@]}"                       \
        -DSHARED=${_shared}                        \
        ../${_realname}-${pkgver}
    make
  done
}

package() {
  for _shared in OFF ON; do
    cd "${srcdir}"/build-shared-${_shared}-${CARCH}
    make install DESTDIR="${pkgdir}"
    mkdir -p "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig"
    echo "
      prefix=${MINGW_PREFIX}
      libdir=\${prefix}/lib
      includedir=\${prefix}/include
      Name: ${_realname}
      URL: ${url}
      Version: ${pkgver}
      Description: ${pkgdesc}
      Cflags: -I\${includedir}
      Libs: -L\${libdir} -l${_realname}
    " | sed '/^\s*$/d;s/^\s*//' > "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/${_realname}.pc"
  done
}
